<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:p="pilferpage" xmlns="http://www.w3.org/1999/xhtml">

	<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" doctype-system="" doctype-public=""/>

	<xsl:template match="/p:page">
		<xsl:element name="html">
			<xsl:element name="head">
				<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
				<style type="text/css">
					.rowLevel1 {padding-left:1em}
					.rowLevel2 {padding-left:2em}
					.rowLevel3 {padding-left:3em}
					.rowLevel4 {padding-left:4em}
					.rowLevel5 {padding-left:5em}
					.rowLevel6 {padding-left:6em}
				</style>
			</xsl:element>
			<xsl:element name="body">
				<xsl:if test="p:top">
					<div id="top"><xsl:apply-templates select="p:top"/></div>
				</xsl:if>
				<xsl:variable name="middleNodes" select="node()[not(self::p:top or self::p:left or self::p:right or self::p:bottom)]"/>
				<xsl:if test="$middleNodes">
					<div id="middle"><xsl:apply-templates select="$middleNodes"/></div>
				</xsl:if>
				<xsl:if test="p:left">
					<div id="left"><xsl:apply-templates select="p:left"/></div>
				</xsl:if>
				<xsl:if test="p:right">
					<div id="right"><xsl:apply-templates select="p:right"/></div>
				</xsl:if>
				<xsl:if test="p:bottom">
					<div id="bottom"><xsl:apply-templates select="p:bottom"/></div>
				</xsl:if>
			</xsl:element>
		</xsl:element>
	</xsl:template>

	<xsl:template match="p:title">
		<xsl:choose>
			<xsl:when test="parent::p:table">
				<caption><xsl:apply-templates/></caption>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="ancestorSections" select="ancestor::p:section"/>
				<xsl:choose>
					<xsl:when test="count($ancestorSections) &gt; 4">
						<h6><xsl:apply-templates/></h6>
					</xsl:when>
					<xsl:when test="count($ancestorSections) &gt; 3">
						<h5><xsl:apply-templates/></h5>
					</xsl:when>
					<xsl:when test="count($ancestorSections) &gt; 2">
						<h4><xsl:apply-templates/></h4>
					</xsl:when>
					<xsl:when test="count($ancestorSections) &gt; 1">
						<h3><xsl:apply-templates/></h3>
					</xsl:when>
					<xsl:when test="count($ancestorSections) &gt; 0">
						<h2><xsl:apply-templates/></h2>
					</xsl:when>
					<xsl:otherwise>
						<h1><xsl:apply-templates/></h1>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>	

	<xsl:template match="p:table">
		<xsl:element name="table">
			<xsl:apply-templates select="p:title"/>
			<!-- browsers should allow out of order headers and footers -->
			<xsl:apply-templates select="p:head"/>
			<xsl:apply-templates select="p:foot"/>
			<xsl:if test="p:row | p:body">
				<tbody>
					<xsl:apply-templates select="p:row | p:body"/>
				</tbody>
			</xsl:if>
		</xsl:element>
	</xsl:template>

	<xsl:template match="p:head">
		<xsl:element name="thead"><xsl:apply-templates/></xsl:element>
	</xsl:template>

	<xsl:template match="p:foot">
		<xsl:element name="tfoot"><xsl:apply-templates/></xsl:element>
	</xsl:template>

	<xsl:template match="p:row">
		<xsl:element name="tr"><xsl:apply-templates select="p:cell"/></xsl:element>
		<xsl:apply-templates select="p:row"/>
	</xsl:template>

	<xsl:template match="p:cell">
		<xsl:variable name="ancestorRows" select="count(ancestor::p:row)"/>
		<xsl:choose>
			<xsl:when test="ancestor::p:head or ancestor::p:foot">
				<xsl:element name="th">
					<xsl:if test="not(preceding-sibling::p:cell) and $ancestorRows &gt; 1">
						<xsl:attribute name="class">rowLevel<xsl:value-of select="$ancestorRows - 1"/></xsl:attribute>
					</xsl:if>
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>
				<xsl:element name="td">
					<xsl:if test="not(preceding-sibling::p:cell) and $ancestorRows &gt; 1">
						<xsl:attribute name="class">rowLevel<xsl:value-of select="$ancestorRows - 1"/></xsl:attribute>
					</xsl:if>
					<xsl:apply-templates/>
				</xsl:element>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="p:image">
		<xsl:if test="ancestor::p:image">
			<xsl:apply-templates/>
		</xsl:if>
		<xsl:element name="img">
			<xsl:attribute name="src"><xsl:value-of select="@url"/></xsl:attribute>
			<xsl:attribute name="alt">
				<xsl:variable name="altText">
					<xsl:apply-templates/>
				</xsl:variable>
				<xsl:value-of select="translate(normalize-space($altText), '&#9;', '')"/>
			</xsl:attribute>
		</xsl:element>
	</xsl:template>

	<xsl:template match="p:abbreviation">
		<xsl:element name="abbrev">
			<xsl:attribute name="title"><xsl:value-of select="@full"/></xsl:attribute>
			<xsl:apply-templates/>
			<xsl:if test="ancestor::p:image">
				<xsl:text>[</xsl:text>
				<xsl:value-of select="@full"/>
				<xsl:text>]</xsl:text>
			</xsl:if>
		</xsl:element>
	</xsl:template>

	<xsl:template match="p:paragraph">
		<xsl:element name="p"><xsl:apply-templates/></xsl:element>
	</xsl:template>

	<xsl:template match="p:link">
		<xsl:element name="a">
			<xsl:attribute name="href"><xsl:value-of select="@url"/></xsl:attribute>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>


</xsl:stylesheet>
